import moment from 'moment';
import { Tooltip } from 'antd';
import { ILoadMapTypeAll } from '@/common';
import pageStyles from '@/common/pageLayout.less';
import { getColorTextLoadMap, getTenantID } from '@/utils';
import { TagSelectNew as TagSelect } from '@/components/TagSelectNew';

/**`1004`待采购*/
export const DCG_SQSTATE = () => `${getTenantID()}${ILoadMapTypeAll?.PURAPPLY_STATE}1004`;

// <<<==========================采购实施状态===========================>>>START
/**`1000`待编制*/
export const DBZ_STATE = () => `${getTenantID()}${ILoadMapTypeAll?.CURRYOUT_STATE}1000`;
/**`1001`文件编制中*/
export const WJBZZ_STATE = () => `${getTenantID()}${ILoadMapTypeAll?.CURRYOUT_STATE}1001`;
/**`1002`编制校准*/
export const BZJZ_STATE = () => `${getTenantID()}${ILoadMapTypeAll?.CURRYOUT_STATE}1002`;
/**`1010`待实施*/
export const BZWC_STATE = () => `${getTenantID()}${ILoadMapTypeAll?.CURRYOUT_STATE}1010`;
/**`1003`招标公告*/
export const ZBGG_STATE = () => `${getTenantID()}${ILoadMapTypeAll?.CURRYOUT_STATE}1003`;
/**`1004`澄清中*/
export const CQXG_STATE = () => `${getTenantID()}${ILoadMapTypeAll?.CURRYOUT_STATE}1004`;
/**`1005`投标统筹*/
export const TBTC_STATE = () => `${getTenantID()}${ILoadMapTypeAll?.CURRYOUT_STATE}1005`;
/**`1006`录入开评标信息*/
export const LRKPB_STATE = () => `${getTenantID()}${ILoadMapTypeAll?.CURRYOUT_STATE}1006`;
/**`1008`采购方式变更中*/
export const CGFSBG_STATE = () => `${getTenantID()}${ILoadMapTypeAll?.CURRYOUT_STATE}1008`;
/**`1007`定标*/
export const DB_STATE = () => `${getTenantID()}${ILoadMapTypeAll?.CURRYOUT_STATE}1007`;
/**`1009`废标*/
export const FB_STATE = () => `${getTenantID()}${ILoadMapTypeAll?.CURRYOUT_STATE}1009`;
// <<<==========================采购实施状态===========================>>>END

/**====================工作流使用===========================START*/
/** 流程类别【category】*/
export const BZ_flowBusinessType = () => `${getTenantID()}${ILoadMapTypeAll.ALLFLOW_TYPE}1004`;
/** 业务Controller【controller】*/
export const BZ_requestController = () => 'purchaseSourceController';
/** 工单类型【orderType】 */
export const BZ_orderType = () => `${getTenantID()}${ILoadMapTypeAll.ALLORDER_TYPE}1014`;
/** 业务流程类别【businessCategory】*/
export const BZ_businessCategory = () => `${getTenantID()}${ILoadMapTypeAll.CURRYOUT_CATEGORY}1000`;
/**====================工作流使用===========================END*/

/**判断值是否有两位小数点，没有则自动补零*/
export const formatDecimal = (val: any) => {
  // 不存在的话就返回'--'
  if (!val) return '--';
  // 将输入值转为数字，以确保处理的是数值类型
  const num = Number(val);
  // 如果转换后的数值是NaN，则直接返回原始值
  if (isNaN(num)) return '--';
  // 使用toFixed方法确保小数点后有两位，然后再转回字符串
  return num.toFixed(2) + '';
};

export const calculate = (a: any, b: any) => {
  a = a || 0;
  b = b || 0;
  // 将b转换为小数，并保留两位小数
  let bDecimal = parseFloat(b?.toFixed(2));
  // 将b的小数点向前移动两位
  let multiplier = bDecimal / 100;
  // 计算a和multiplier的乘积，并保留两位小数
  let c = (parseFloat(a) * multiplier)?.toFixed(2);
  // 返回c
  return c || '0.00';
};

/**项目属性*/
const handle_xmsx = (resObj) => {
  if (resObj?.supplyType == `${getTenantID()}${ILoadMapTypeAll?.PURCHASE_CATEGORY}1000`) {
    delete resObj?.sciequipFlag;
    delete resObj?.importFlag;
    delete resObj?.productFlag;
    delete resObj?.productName;
  } else if (
    resObj?.supplyType == `${getTenantID()}${ILoadMapTypeAll?.PURCHASE_CATEGORY}1001` ||
    resObj?.supplyType == `${getTenantID()}${ILoadMapTypeAll?.PURCHASE_CATEGORY}1002`
  ) {
    if (resObj?.productFlag == 0) {
      delete resObj?.productName;
    }
  }
  return resObj;
};

/**分包处理*/
const handle_fbcl = (resObj) => {
  if (resObj?.packFlag == 0) {
    delete resObj?.packScale1;
    delete resObj?.packScale;
    delete resObj?.packContent;
    delete resObj?.packOther;
  }
  return resObj;
};

/**统一合同范本*/
const handle_tyhtfb = (resObj) => {
  // if (resObj?.templateFlag == 1) {
  //   delete resObj?.bidTempFileURL;
  // } else if (resObj?.templateFlag == 0) {
  //   delete resObj?.contract;
  // }
  return resObj;
};

/**履约保证金*/
const handle_lybzj = (resObj) => {
  if (resObj?.bidMarginFlag == 0) {
    delete resObj?.bidMargin;
    delete resObj?.bidMarginTime;
  }
  return resObj;
};

/**评标办法*/
const handle_pbbf = (resObj) => {
  if (resObj?.evalStyle == `${getTenantID()}${ILoadMapTypeAll?.EVALSTYLE_TYPE}1003`) {
    delete resObj?.evalFileURL;
  }
  return resObj;
};

/**是否现场考察*/
const handle_xckc = (resObj) => {
  if (resObj?.siteVisitTime) {
    // 处理时间
    resObj.siteVisitTime = moment(resObj.siteVisitTime)?.format('YYYY-MM-DD 00:00:00');
  }

  if (resObj?.siteVisitFlag == 0) {
    delete resObj?.siteVisitTime;
    delete resObj?.siteVisitAddress;
  }

  return resObj;
};

/**开标前答疑会*/
const handle_kbqdyh = (resObj) => {
  if (resObj?.meetTime) {
    // 处理时间
    resObj.meetTime = moment(resObj.meetTime)?.format('YYYY-MM-DD 00:00:00');
  }

  if (resObj?.meetFlag == 0) {
    delete resObj?.meetTime;
    delete resObj?.meetAddress;
  }
  return resObj;
};

/**是否需要样品*/
const handle_sfxyyp = (resObj) => {
  if (resObj?.sampleFlag == 0) {
    delete resObj?.sampleCheck;
    delete resObj?.sampleMake;
    delete resObj?.sampleReturn;
    delete resObj?.sampleSeal;
    delete resObj?.sampleDeliver;
    delete resObj?.sampleOther;
  } else if (resObj?.sampleFlag == 1) {
    if (resObj?.sampleCheck == 0) {
      // 相关检测报告不需要
      delete resObj?.sampleReturn;
      delete resObj?.sampleSeal;
      delete resObj?.sampleDeliver;
      delete resObj?.sampleOther;
    }
  }
  return resObj;
};

/**处理`公开招标` &&  `邀请招标`*/
export const handleForm_GKZB = (resObj) => {
  if (!resObj) return {};
  resObj = handle_xmsx(resObj);
  resObj = handle_fbcl(resObj);
  resObj = handle_pbbf(resObj);
  resObj = handle_tyhtfb(resObj);
  resObj = handle_xckc(resObj);
  resObj = handle_kbqdyh(resObj);
  resObj = handle_sfxyyp(resObj);
  resObj = handle_lybzj(resObj);
  return resObj;
};

/**处理`竞争性谈判方式`*/
export const handleForm_JZXTP = (resObj) => {
  if (!resObj) return {};
  resObj = handle_xmsx(resObj);
  resObj = handle_fbcl(resObj);
  resObj = handle_tyhtfb(resObj);
  resObj = handle_lybzj(resObj);
  return resObj;
};

/**处理`竞争性磋商方式`*/
export const handleForm_JZXCS = (resObj) => {
  if (!resObj) return {};
  resObj = handle_xmsx(resObj);
  resObj = handle_fbcl(resObj);
  resObj = handle_tyhtfb(resObj);
  resObj = handle_xckc(resObj);
  resObj = handle_kbqdyh(resObj);
  resObj = handle_lybzj(resObj);
  return resObj;
};

/**处理`询价采购方式`*/
export const handleForm_XJCG = (resObj) => {
  if (!resObj) return {};
  resObj = handle_xmsx(resObj);
  resObj = handle_fbcl(resObj);
  resObj = handle_tyhtfb(resObj);
  resObj = handle_lybzj(resObj);
  return resObj;
};

/**处理`框架协议采购方式`*/
export const handleForm_KJXYCG = (resObj) => {
  if (!resObj) return {};
  resObj = handle_xmsx(resObj);
  resObj = handle_tyhtfb(resObj);
  resObj = handle_xckc(resObj);
  resObj = handle_kbqdyh(resObj);
  resObj = handle_sfxyyp(resObj);
  return resObj;
};

/**处理`单一来源`*/
export const handleForm_DYLY = (resObj) => {
  if (!resObj) return {};
  resObj = handle_xmsx(resObj);
  resObj = handle_fbcl(resObj);
  resObj = handle_tyhtfb(resObj);
  resObj = handle_lybzj(resObj);
  return resObj;
};

/**处理`便捷询价方式`*/
export const handleForm_BJXJ = (resObj) => {
  if (!resObj) return {};
  resObj = handle_xmsx(resObj);
  resObj = handle_tyhtfb(resObj);
  resObj = handle_lybzj(resObj);
  return resObj;
};

/**处理`便捷磋商方式`*/
export const handleForm_BJCS = (resObj) => {
  if (!resObj) return {};
  resObj = handle_xmsx(resObj);
  resObj = handle_tyhtfb(resObj);
  resObj = handle_tyhtfb(resObj);
  return resObj;
};

/**处理`便捷框架协议采购方式`*/
export const handleForm_BJKJXYCG = (resObj) => {
  if (!resObj) return {};
  resObj = handle_xmsx(resObj);
  resObj = handle_lybzj(resObj);
  resObj = handle_tyhtfb(resObj);
  return resObj;
};

/** 处理政府类的采购方式 */
export const handleForm_ZF = (resObj) => {
  resObj = handle_xmsx(resObj);
  return resObj;
};

/**表单过滤方法*/
export const gl_formFunc = (resObj) => {
  if (resObj?.supplyType == `${getTenantID()}${ILoadMapTypeAll?.PURCHASE_CATEGORY}1000`) {
    // delete resObj?.sciequipFlag;
    resObj.sciequipFlag = 0;
    delete resObj?.importFlag;
    delete resObj?.productFlag;
    delete resObj?.productName;
  }
  if (
    resObj?.supplyType == `${getTenantID()}${ILoadMapTypeAll?.PURCHASE_CATEGORY}1001` ||
    resObj?.supplyType == `${getTenantID()}${ILoadMapTypeAll?.PURCHASE_CATEGORY}1002`
  ) {
    if (resObj?.productFlag == 0) {
      delete resObj?.productName;
    }
  }
  if (resObj?.packFlag == 0) {
    delete resObj?.packScale1;
    delete resObj?.packScale;
    delete resObj?.packContent;
    delete resObj?.packOther;
  }
  if (resObj?.bidMarginFlag == 0) {
    delete resObj?.bidMargin;
    delete resObj?.bidMarginTime;
  }
  if (resObj?.evalStyle == `${getTenantID()}${ILoadMapTypeAll?.EVALSTYLE_TYPE}1003`) {
    delete resObj?.evalFileURL;
  }
  if (resObj?.siteVisitTime) {
    // 处理时间
    resObj.siteVisitTime = moment(resObj.siteVisitTime)?.format('YYYY-MM-DD 00:00:00');
  }
  if (resObj?.siteVisitFlag == 0) {
    delete resObj?.siteVisitTime;
    delete resObj?.siteVisitAddress;
  }
  if (resObj?.meetTime) {
    // 处理时间
    resObj.meetTime = moment(resObj.meetTime)?.format('YYYY-MM-DD 00:00:00');
  }
  if (resObj?.meetFlag == 0) {
    delete resObj?.meetTime;
    delete resObj?.meetAddress;
  }
  if (resObj?.sampleFlag == 0) {
    delete resObj?.sampleCheck;
    delete resObj?.sampleMake;
    delete resObj?.sampleReturn;
    delete resObj?.sampleSeal;
    delete resObj?.sampleDeliver;
    delete resObj?.sampleOther;
  }
  if (resObj?.sampleFlag == 1) {
    if (resObj?.sampleCheck == 0) {
      // 相关检测报告不需要
      delete resObj?.sampleReturn;
      delete resObj?.sampleSeal;
      delete resObj?.sampleDeliver;
      delete resObj?.sampleOther;
    }
  }
  return resObj;
};

/** columns内容渲染的方法【非字典】 */
export const ColumnsRenderFun = ({ isTooltip = true, text, textStyle = {}, event = undefined, record = {} }: Record<string, any>) => {
  return (
    <>
      <Tooltip title={isTooltip ? text : undefined}>
        <span className={pageStyles['over-flow-hidden']} onClick={event ? () => event(record) : undefined} style={textStyle}>
          {text == null || text == undefined || text == '' ? '--' : text}
        </span>
      </Tooltip>
    </>
  );
};

/** columns内容渲染的方法【字典】 */
export const DictionaryRenderFun = ({ isTooltip = false, text, textStyle = {}, event = undefined, record = {} }: Record<string, any>) => {
  const { colorText, color } = getColorTextLoadMap(text);
  return (
    <>
      <Tooltip title={isTooltip ? text : undefined}>
        <span className={pageStyles['over-flow-hidden']} onClick={event ? () => event(record) : undefined} style={{ color, ...textStyle }}>
          {colorText == null || colorText == undefined || colorText == '' ? '--' : colorText}
        </span>
      </Tooltip>
    </>
  );
};

/**
 * @param label 表单label
 * @param name 表单field
 * @param required 是否必填
 * @param value 单选值
 * @returns
 */
export const TagSelectRender = (label: string, name: string, required: boolean = false, value: number = 0) => {
  return (
    <TagSelect
      colSpan={12}
      formLabel={label}
      formRequired={required}
      formName={name}
      defalultKeys={isNaN(value) ? value : 0}
      radioList={[
        {
          value: 1,
          name: '是',
        },
        {
          value: 0,
          name: '否',
        },
      ]}
    />
  );
};

/**
 * setTableRowClassName 设置表格单双行类名
 */
export const setTableRowClassName = (_: Record<string, any>, index: number) => {
  return index % 2 == 0 ? 'double-row' : 'default-row';
};

/**
 * 排序
 * */
export const getSorter = (prev: Record<string, any>, next: Record<string, any>): number => (prev > next ? 1 : -1);
